local skel = fk.CreateSkill {
  name = "rmt__kouyong",
}

Fk:loadTranslationTable{
  ["rmt__kouyong"] = "寇勇",
  [":rmt__kouyong"] = "当你攻击范围内的角色受到伤害后，你可以选择一项：1.视为对其使用一张【过河拆桥】，然后你可以对你或其使用"..
  "因此被弃置的牌；2.视为对其使用一张【顺手牵羊】，然后其摸一张牌。",

  ["#rmt__kouyong-invoke"] = "寇勇：你可以视为对 %dest 使用【过河拆桥】或【顺手牵羊】",
  ["#rmt__kouyong-use"] = "寇勇：你可以对你或 %dest 使用被弃置的牌",

  ["$rmt__kouyong1"] = "蹂稼穑、夺妇女，勇力傍身，有何不可！",
  ["$rmt__kouyong2"] = "大将克敌，焚掠肆杀，士众自然用命。",
}

skel:addEffect(fk.Damaged, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and not target.dead and player:inMyAttackRange(target) and not target:isAllNude() and
      not (player:isProhibited(target, Fk:cloneCard("dismantlement")) and player:isProhibited(target, Fk:cloneCard("snatch")))
  end,
  on_cost = function(self, event, target, player, data)
    local choices = table.filter({"dismantlement", "snatch"}, function (name)
      return not player:isProhibited(target, Fk:cloneCard(name))
    end)
    table.insert(choices, "Cancel")
    local choice = player.room:askToChoice(player, {
      choices = choices, all_choices = {"dismantlement", "snatch", "Cancel"}, skill_name = skel.name,
      prompt = "#rmt__kouyong-invoke::"..target.id,
    })
    if choice ~= "Cancel" then
      event:setCostData(self, { choice = choice } )
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = event:getCostData(self).choice
    local card = Fk:cloneCard(choice)
    card.skillName = skel.name
    local use = {
      from = player,
      tos = { target },
      card = card,
    }
    if choice == "dismantlement" then
      use.extra_data = use.extra_data or {}
      use.extra_data.rmt__kouyong = player.id
    end
    room:useCard(use)
    if choice == "dismantlement" and not player.dead then
      local cards = use.extra_data.rmt__kouyong_cards or {}
      cards = table.filter(cards, function (id)
        local c = Fk:getCardById(id)
        return table.contains(room.discard_pile, id) and
          (player:canUseTo(c, target, {bypass_times = true}) or player:canUseTo(c, player, {bypass_times = true}))
      end)
      if #cards > 0 then
        local use_data = {bypass_times = true, exclusive_targets = {player.id, target.id}}
        room:askToUseRealCard(player, {
          skill_name = skel.name, prompt = "#rmt__kouyong-use::"..target.id,
          pattern = cards, expand_pile = cards, extra_data = use_data, skip = false,
        })
      end
    elseif choice == "snatch" and not target.dead then
      target:drawCards(1, skel.name)
    end
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player.dead then return false end
    local use_event = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
    if use_event == nil then return false end
    local use = use_event.data
    if (use.extra_data or Util.DummyTable).rmt__kouyong == player.id then
      local cards = {}
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard and move.skillName == "dismantlement_skill" then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(player.room.discard_pile, info.cardId) then
              table.insertIfNeed(cards, info.cardId)
            end
          end
        end
      end
      if #cards > 0 then
        cards = player.room.logic:moveCardsHoldingAreaCheck(cards)
        if #cards > 0 then
          event:setCostData(self, cards)
          return true
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local use_event = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
    if use_event == nil then return end
    local use = use_event.data
    use.extra_data = use.extra_data or {}
    use.extra_data.rmt__kouyong_cards = event:getCostData(self)
  end,
})

return skel
